package com.cj.framework.widget;

import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.yuanduan.mplus.R;


/**
 * Created by Administrator on 2016/9/7.
 */
public class CircleView extends View {
    private int color1;
    private int color2;
    private int point1;
    private int point2;
    private float currentPoint1=0;
    private float currentPoint2=0;
    private String name1;
    private String name2;
    private Paint paint1;
    private Paint paint2;
    private Paint paintX;
    public CircleView(Context context) {
        this(context,null);
    }

    public CircleView(Context context, AttributeSet attrs) {
        this(context,attrs,0);
    }

    public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        //获取xml变量到字段
        TypedArray ta=context.obtainStyledAttributes(attrs, R.styleable.CircleView);
        name1=ta.getString(R.styleable.CircleView_name1);
        name2=ta.getString(R.styleable.CircleView_name2);
        color1=ta.getColor(R.styleable.CircleView_color1, Color.YELLOW);
        color2=ta.getColor(R.styleable.CircleView_color2,Color.BLUE);
        ta.recycle();

        //初始化笔
        paint1=new Paint(Paint.ANTI_ALIAS_FLAG);
        paint1.setColor(color1);
        paint1.setTextSize(40);
        paint1.setStyle(Paint.Style.STROKE);

        paint2=new Paint(Paint.ANTI_ALIAS_FLAG);
        paint2.setColor(color2);
        paint2.setTextSize(40);
        paint2.setStyle(Paint.Style.STROKE);

        paintX=new Paint(Paint.ANTI_ALIAS_FLAG);
        paintX.setColor(Color.BLACK);
        paintX.setStyle(Paint.Style.STROKE);
        paintX.setTextSize(40);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //计算宽，高(宽<高)
        int width=getWidth()-getPaddingLeft()-getPaddingRight();
        int height=getHeight()-getPaddingBottom()-getPaddingTop();
        //画字
        float textXWidth=paintX.measureText("/");
        float point1Width=paint1.measureText(point1+"");
        float point2Width=paint2.measureText(point2+"");

        if(point1!=0){
            paint1.setTextSize(40);
            paint1.setStrokeWidth(0);
            canvas.drawText(point1+"",width/2.0f-textXWidth/2.0f-point1Width,height/2.0f-(paint1.ascent()+paint1.descent())/2.0f,paint1);
        }
        if(point2!=0){
            paint2.setStrokeWidth(0);
            canvas.drawText(point2+"",width/2.0f+textXWidth/2.0f,height/2.0f-(paint2.ascent()+paint2.descent())/2.0f,paint2);
        }

        if(point1!=0&&point2!=0){
            canvas.drawText("/",width/2.0f-textXWidth/2.0f,height/2.0f-((paintX.ascent()+paintX.descent())/2.0f),paintX);
        }
        //画圈
        //画外圆环
        paint2.setStrokeWidth(20);
        RectF rectF1=new RectF(10,height/2.0f-width/2.0f+10,width-10,height/2.0f+width/2.0f-10);
        canvas.drawArc(rectF1,-90,currentPoint2/100f*360,false,paint2);
        //画内圆环
        paint1.setStrokeWidth(20);
        RectF  rectF2=new RectF(38,height/2.0f-(width/2.0f-38),width-38,height/2.0f+(width/2.0f-38));
        canvas.drawArc(rectF2,-90f,-1*currentPoint1/100f*360,false,paint1);
    }

    //动画开始;
    public void start(int point1,int point2){
        this.point1=point1;
        this.point2=point2;
        ValueAnimator valueAnimator=ValueAnimator.ofFloat(0.2f,point1);
        valueAnimator.setDuration(2000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                currentPoint1= (float) valueAnimator.getAnimatedValue();
                invalidate();
            }
        });
        ValueAnimator valueAnimator1=ValueAnimator.ofFloat(0,this.point2);
        valueAnimator1.setDuration(2000);
        valueAnimator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                currentPoint2= (float) valueAnimator.getAnimatedValue();
            }
        });
        AnimatorSet set=new AnimatorSet();
        set.play(valueAnimator1).with(valueAnimator);
        set.start();
    }

}
